package com.example.goodrubish.Garbage;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.goodrubish.R;
import com.example.goodrubish.login;

public class OnboardingActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private Button btnNext, btnSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onboarding);

        viewPager = findViewById(R.id.view_pager);
        btnNext = findViewById(R.id.btn_next);
        btnSkip = findViewById(R.id.btn_skip);

        viewPager.setAdapter(new OnboardAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT));

        btnNext.setOnClickListener(v -> viewPager.setCurrentItem(viewPager.getCurrentItem() + 1));
        btnSkip.setOnClickListener(v -> {
            // 跳过并进入主页面/登录页面
            Intent intent = new Intent(OnboardingActivity.this, login.class);
            startActivity(intent);
            finish();
        });

        // 更新按钮文本逻辑
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                if (position == 3) { // 最后一页
                    btnNext.setText("立即体验");
                    btnNext.setOnClickListener(v -> {
                        // 当点击“立即体验”时跳转到主页
                        Intent intent = new Intent(OnboardingActivity.this, login.class);
                        startActivity(intent);
                        finish(); // 关闭当前活动页
                    });
                } else {
                    btnNext.setText("下一步");
                    btnNext.setOnClickListener(v -> viewPager.setCurrentItem(viewPager.getCurrentItem() + 1));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }
}
